package com.juku2024.juku

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.webkit.*
import com.juku2024.juku.base.BaseActivity
import com.juku2024.juku.network.retrofit.RetrofitUtils
import com.juku2024.juku.tools.AppUtils

class CommonProblemActivity: BaseActivity() {

    private lateinit var webView: WebView

    override fun getLayoutResId(): Int {
        return R.layout.activity_common_problem
    }

    override fun initView() {
        super.initView()
        webView = findViewById(R.id.webView)
    }

    override fun init(savedInstanceState: Bundle?) {
        initWebView(webView)
        val url = String.format("%shtml/faq/%s.html", RetrofitUtils.BASE_URL, AppUtils.method03())
        webView.loadUrl(url)
    }

    override fun onResume() {
        super.onResume()
        webView.onResume()
    }

    override fun onPause() {
        super.onPause()
        webView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        webView.destroy()
    }

    private fun initWebView(webView: WebView) {
        webView.webViewClient = object : WebViewClient() {

            override fun shouldOverrideUrlLoading(view: WebView?, request: WebResourceRequest?): Boolean {
                val url = request?.url?.toString()
                if (url == null || TextUtils.isEmpty(url)) {
                    return false
                }
                return if (Build.VERSION.SDK_INT < 26) {
                    view?.loadUrl(url)
                    true
                } else {
                    false
                }
            }
        }
        webView.setBackgroundColor(Color.BLACK)
        //声明WebSettings子类
        val webSettings = webView.settings
        //如果访问的页面中要与Javascript交互，则webview必须设置支持Javascript
        webSettings.javaScriptEnabled = true
        //设置自适应屏幕，两者合用
        webSettings.useWideViewPort = false //将图片调整到适合webview的大小
        webSettings.loadWithOverviewMode = false // 缩放至屏幕的大小
        //缩放操作
        webSettings.setSupportZoom(true) //支持缩放，默认为true。是下面那个的前提。
        webSettings.builtInZoomControls = true //设置内置的缩放控件。若为false，则该WebView不可缩放
        webSettings.displayZoomControls = false //隐藏原生的缩放控件
        //其他细节操作
        webSettings.cacheMode = WebSettings.LOAD_NO_CACHE //关闭webview中缓存
        webSettings.allowFileAccess = false //设置可以访问文件
        webSettings.javaScriptCanOpenWindowsAutomatically = false //支持通过JS打开新窗口
        webSettings.loadsImagesAutomatically = true //支持自动加载图片
        webSettings.domStorageEnabled = true
        webSettings.setSupportMultipleWindows(false)
    }
}